import { getTime, parseTime, formatTime, formatBankCard, formatPhone, formatIndentity } from '@/utils'

const filters = {
  /**
   * 获取时间戳
   * @param {Number} start 距离当前天数
   * @returns {String} 时间戳
   */
  getTime,
  /**
   * 时间戳转字符串
   * @param {(Object|string|number)} time
   * @param {string} cFormat
   * @returns {string | null}
   */
  parseTime,
  /**
   * 格式化时间
   * @param {number} time
   * @param {string} option
   * @returns {string}
   */
  formatTime,
  /**
   * 返回银行卡号格式
   * @param {String} bankCard
   * @return {String} 1234556 => 1234 556
   */
  formatBankCard,
  /**
   * 返回电话号码格式
   * @param {String} str  11位电话号码
   * @return {String} 15221865665 => 152 **** 5665
   */
  formatPhone,
  /**
   * 返回身份证号码格式
   * @param {String} str 18位身份证号码
   * @return {String} 11000119901010123X => **** **** **** 123X
   */
  formatIndentity,
  /**
   * 大写第一个字符
   * @param {String} string
   * @return {String} abcdef => Abcdef
   */
  uppercaseFirst(string) {
    return string.charAt(0).toUpperCase() + string.slice(1)
  },
  // 分页大小
  pageSizes: [10, 50, 100, 500],
  // 默认分页大小
  defaultPageSize: 10,
  // 文字占位
  renderText(text) {
    return text || '--'
  },
  // 图片占位
  renderImage(image) {
    return image || '/images/no-image.png'
  },
  // 日历时间
  calendarTime(date) {

  },
  /**
   * 强制保留2位小数 不足补零
   * @param {Number} number
   * @return {String} 10.1 => 10.10
   */
  toDecimal(number) {
    if ([undefined, null, ''].includes(number) || isNaN(number)) return ''
    number = Math.round(parseFloat(number) * 100) / 100
    const value = String(number).split('.')
    if (value.length === 1) {
      return String(number) + '.00'
    }
    if (value.length > 1 && value[1].length < 2) {
      return String(number) + '0'
    }
    return number
  },
  /**
   * 三位逗号
   * @param {Number} num
   * @return {String} 10000 => "10,000"
   */
  toThousandFilter(num) {
    return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  },
  /**
   * 表单校验 失败的第一项自动获取焦点
   */
  validateFocus() {
    setTimeout(() => {
      const isError = document.getElementsByClassName('is-error')
      isError[0].querySelector('input').focus()
    }, 100)
  },
  /**
   * 当前时间
   * @return {String} 2021-10-01 14:22:24
   */
  currentTime() {
    const yy = new Date().getFullYear()
    const mm = new Date().getMonth() + 1
    const dd = new Date().getDate()
    const hh = new Date().getHours()
    const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
    const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
    return yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
  },
  /**
   * 起止日期：指定N天前 - 今天
   * @description:
   * @param {Number} day
   * @return {Array}
   */
  getDates(day) {
    const startTime = new Date()
    const endTime = new Date()
    const arr = []
    let Stime = ''
    let Etime = ''
    endTime.setTime(endTime.getTime() - 1000 * 60 * 60 * 24 * 1)
    startTime.setTime(startTime.getTime() - 1000 * 60 * 60 * 24 * day)
    Stime = startTime.getFullYear() + '-' + this.doHandleMonth(startTime.getMonth() + 1) + '-' + this.doHandleMonth(startTime.getDate())
    Etime = endTime.getFullYear() + '-' + this.doHandleMonth(endTime.getMonth() + 1) + '-' + this.doHandleMonth(endTime.getDate())
    arr.push(Stime)
    day === 0 ? arr.push(Stime) : arr.push(Etime)
    return arr
  },
  doHandleMonth(month) {
    let m = month
    if (month.toString().length === 1) {
      m = '0' + month
    }
    return m
  },
  // 选择器选项
  pickerOptions: {
    disabledDate(time) {
      return time.getTime() > Date.now()
    },
    shortcuts: [
      {
        text: '最近一周',
        onClick(picker) {
          const end = new Date()
          const start = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
          picker.$emit('pick', [start, end])
        }
      },
      {
        text: '最近一个月',
        onClick(picker) {
          const end = new Date()
          const start = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
          picker.$emit('pick', [start, end])
        }
      },
      {
        text: '最近三个月',
        onClick(picker) {
          const end = new Date()
          const start = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
          picker.$emit('pick', [start, end])
        }
      }
    ]
  }
}

filters.install = Vue => {
  const keys = Object.keys(filters)
  Vue.prototype.utils = Object.assign(filters)
  for (const key of keys) {
    if (typeof filters[key] === 'function') Vue.filter(key, filters[key])
  }
}

export default filters
